//
//  PS_ButtonMenu.m
//  PolicySystem
//
//  Created by 丹丹 on 2018/10/25.
//  Copyright © 2018年 xuezi. All rights reserved.
//

#import "PS_ButtonMenu.h"

#define MENUNUM  4


@interface PS_ButtonMenu ()

@property (nonatomic , strong) NSMutableArray *imageArr;
@property (nonatomic , strong) NSMutableArray *labelArr;

@end
@implementation PS_ButtonMenu
-(instancetype)initWithFrame:(CGRect)frame{
    
    
    if (self = [super initWithFrame:frame]) {
        
        UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        backView.backgroundColor = [UIColor whiteColor];
        [self addSubview:backView];
        
        CGFloat width = frame.size.width/MENUNUM;
        CGFloat height = frame.size.height;
        for (int i=0; i<MENUNUM; i++) {
            
            UIView * itemView = [[UIView alloc]initWithFrame:CGRectMake(width*i, 0, width,height)];
            itemView.backgroundColor = [UIColor whiteColor];
            [backView addSubview:itemView];
            
            UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake((itemView.frame.size.width - 25)/2, 25, 25, 25)];
            image.backgroundColor = [UIColor clearColor];
            image.userInteractionEnabled = YES;
            [itemView addSubview:image];
            
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(image.frame)+10, itemView.frame.size.width, 20)];
            label.textAlignment = NSTextAlignmentCenter;
            label.font = [UIFont systemFontOfSize:15];
            label.textColor = RGBCOLOR(51, 51, 51);
            [itemView addSubview:label];
            
            UIButton *btn = [[UIButton alloc]init];
            btn.tag = 10000+i;
            btn.backgroundColor = [UIColor clearColor];
            btn.frame = CGRectMake(width*i, 0, width,height);
            [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
            [backView addSubview:btn];
            

            [self.imageArr addObject:image];
            [self.labelArr addObject:label];
            

        }
    }
        
    return self;
}
    

- (NSMutableArray *)imageArr{
    if (!_imageArr) {
        _imageArr = [NSMutableArray array];
    }
    return _imageArr;
}
- (NSMutableArray *)labelArr{
    if (!_labelArr) {
        _labelArr = [NSMutableArray array];
    }
    return _labelArr;
}

-(void)btnClick:(UIButton *)sender{
    
    
    NSInteger tag = sender.tag - 10000;
    
    NSLog(@"---%ld---",tag+1);
    
    if ([self.delegate respondsToSelector:@selector(menuViewSelectedIndex:)]) {
        [self.delegate menuViewSelectedIndex:tag+1];
    }
    
}
-(void)setImgsArr:(NSArray *)imgsArr{
    
    for (int i=0; i<imgsArr.count; i++) {
        UIImageView *imageView = self.imageArr[i];
        imageView.image = [UIImage imageNamed:imgsArr[i]];
    }
}

-(void)setTextArr:(NSArray *)textArr{
    
    for (int i=0; i<textArr.count; i++) {
        UILabel *lab = self.labelArr[i];
        lab.text = textArr[i];
    }
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
